import WeaponPowerUp from './WeaponPowerUp.js'
import ShieldPowerUp from './ShieldPowerUp.js'
import LifePowerUp from './LifePowerUp.js'
import BombPowerUp from './BombPowerUp.js'
import SlowPowerUp from './SlowPowerUp.js'

// 道具工厂类
export default class PowerUpFactory {
  // 道具类型概率
  static TYPE_PROBABILITY = {
    weapon: 0.4, // 40%几率生成武器升级道具
    shield: 0.25, // 25%几率生成护盾道具
    life: 0.15, // 15%几率生成额外生命道具
    bomb: 0.1, // 10%几率生成清屏炸弹道具
    slow: 0.1, // 10%几率生成减速道具
  }

  // 创建道具
  static createPowerUp(x, y, type = null) {
    // 如果没有指定类型，则随机生成
    if (!type) {
      type = this.getRandomType()
    }

    // 根据类型创建对应的道具
    switch (type) {
      case 'weapon':
        return new WeaponPowerUp(x, y)
      case 'shield':
        return new ShieldPowerUp(x, y)
      case 'life':
        return new LifePowerUp(x, y)
      case 'bomb':
        return new BombPowerUp(x, y)
      case 'slow':
        return new SlowPowerUp(x, y)
      default:
        return new WeaponPowerUp(x, y)
    }
  }

  // 获取随机道具类型
  static getRandomType() {
    const random = Math.random()
    let cumulativeProbability = 0

    for (const [type, probability] of Object.entries(this.TYPE_PROBABILITY)) {
      cumulativeProbability += probability
      if (random < cumulativeProbability) {
        return type
      }
    }

    // 默认返回武器升级道具
    return 'weapon'
  }
}
